#include "wxshtmlwindow.h"
#include <wx/html/htmlwin.h>

namespace {
  wxsRegisterItem<wxsHtmlWindow> Reg( _T( "HtmlWindow" ), _T( "浏览器" ), wxsTWidget, _T( "对话框" ), 60 );
  WXS_ST_BEGIN( wxsHtmlWindowStyles, _T( "wxHW_SCROLLBAR_AUTO" ) )
  WXS_ST_CATEGORY( "wxHtmlWindow" )
  WXS_ST( wxHW_SCROLLBAR_NEVER )
  WXS_ST( wxHW_SCROLLBAR_AUTO )
  WXS_ST( wxHW_NO_SELECTION )
  WXS_ST_DEFAULTS()
  WXS_ST_END()
  WXS_EV_BEGIN( wxsHtmlWindowEvents )
  WXS_EV_END()
}

wxsHtmlWindow::wxsHtmlWindow( wxsItemResData* Data ):
  wxsWidget( Data, &Reg.Info, wxsHtmlWindowEvents, wxsHtmlWindowStyles ) {
}

void wxsHtmlWindow::OnBuildCreatingCode() {
  switch( GetLanguage() ) {
    case wxsCPP: {
      AddHeader( _T( "<wx/html/htmlwin.h>" ), GetInfo().ClassName, 0 );
      Codef( _T( "%C(%W, %I, %P, %S, %T, %N);\n" ) );
      if( Borders.Value ) {
        Codef( _T( "%ASetBorders(%s);\n" ), Borders.GetPixelsCode( GetCoderContext() ).wx_str() );
      }
      if( !Url.empty() ) {
        Codef( _T( "%ALoadPage(%t);\n" ), Url.wx_str() );
      } else if( !HtmlCode.empty() ) {
        Codef( _T( "%ASetPage(%t);\n" ), HtmlCode.wx_str() );
      }
      BuildSetupWindowCode();
      break;
    }
    case wxsUnknownLanguage:
    default: {
      wxsCodeMarks::Unknown( _T( "wxsHtmlWindow::OnBuildCreatingCode" ), GetLanguage() );
    }
  }
}

wxObject* wxsHtmlWindow::OnBuildPreview( wxWindow* Parent, long Flags ) {
  wxHtmlWindow* Preview = new wxHtmlWindow( Parent, GetId(), Pos( Parent ), Size( Parent ), Style() );
  if( Borders.Value ) {
    Preview->SetBorders( Borders.GetPixels( Parent ) );
  }
  if( !Url.empty() ) {
    if( Flags & pfExact ) {
      Preview->LoadPage( Url );
    } else {
      Preview->SetPage(
        wxString( _T( "<body><center>" ) ) +
        _T( "Following url will be used:" ) +
        _T( "<br>" ) + Url + _T( "</center></body>" ) );
    }
  } else if( !HtmlCode.empty() ) {
    Preview->SetPage( HtmlCode );
  }
  return SetupWindow( Preview, Flags );
}

void wxsHtmlWindow::OnEnumWidgetProperties( M_Code_unused long Flags ) {
  WXS_SHORT_STRING( wxsHtmlWindow, Url, _T( "Url" ), _T( "url" ), _T( "" ), false )
  WXS_STRING( wxsHtmlWindow, HtmlCode, _T( "Html Code" ), _T( "htmlcode" ), _T( "" ), false )
  WXS_DIMENSION( wxsHtmlWindow, Borders, _T( "Borders" ), _T( "Borders in Dialog Units" ), _T( "borders" ), 0, false )
}
